//
//  create_enc_service_request.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#ifndef create_enc_service_request_hpp
#define create_enc_service_request_hpp
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/client_processor.hpp"
#include "components/encrypt/aes_service.hpp"

namespace components{
namespace protocol{



class create_enc_service_request :public boost::enable_shared_from_this<create_enc_service_request>, private boost::noncopyable
{
private:
    std::string name_;

    ba::io_service& ios_;

    crypto_buf_t& iv_;

    crypto_buf_t& key_;

    std::string encrypt_buf_;
    stats_message_client_processor &message_processor_;
    create_enc_service_request (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key, stats_message_client_processor &message_processor) ;
    boost::shared_ptr<components::encrypt::aes_service> aes_service_;
public:
    typedef boost::shared_ptr<create_enc_service_request> pointer;

    bool cancel () ;

    static boost::shared_ptr<create_enc_service_request> create (ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key,stats_message_client_processor &message_processor) ;

    virtual  ~create_enc_service_request () ;

    template<typename Handler >
    bool exec(stats_message_t& req, std::string& send_buf, Handler& handler) {
        try{
            LOG(INFO)<<name_<<"::"<<__FUNCTION__;
            DLOG(INFO)<<"send_buf:\t"<<send_buf ;
            udp_endpoint_t peer;
            bs::error_code ec;
            message_processor_(req,peer,ec);
            encrypt_buf_=req.SerializeAsString();
            aes_service_->enc_key(iv_,key_);
            aes_service_->dec_key(iv_,key_);
            bool ret=aes_service_->exec(components::encrypt::aes_service::encrypt_op,encrypt_buf_,send_buf,handler);

            return ret;

        }
        catch(std::exception &e){
            LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
            cancel();
            return false;
        }
        
    }
};

}//end namespace protocol
}//end namespace components

#endif /*#ifdef create_enc_service_request_hpp*/
